package com.etv.launcher.common.utils;

import org.json.JSONArray;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ObjectUtil {

    /**
     * 拷贝对象
     * @param object
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T> T copyProperties(Object object, Class<T> clazz){
        return cn.hutool.core.bean.BeanUtil.copyProperties(object, clazz);
    }

    public static void copyNonNullProperties(Object source, Object target) {
        // 确保两个对象都是非空的
        if (source == null || target == null) {
            return;
        }

        // 获取对象的Class
        Class<?> clazz = source.getClass();
        if (!clazz.isAssignableFrom(target.getClass())) {
            return;
        }

        // 获取所有可访问的字段
        Field[] fields = clazz.getDeclaredFields();
        AccessibleObject.setAccessible(fields, true); // 设置为可以访问私有字段

        try {
            for (Field field : fields) {
                // 获取源对象的字段值
                Object value = field.get(source);

                // 只复制非空的属性
                if (value != null) {
                    field.set(target, value);
                }
            }
        } catch (Exception e) {

        }

    }

    /**
     * 拷贝集合
     *
     * @param sourceList
     * @param targetClass
     * @param <T>
     * @param <S>
     * @return
     */
    public static <T, S> List<T> copyPropertiesToList(List<S> sourceList, Class<T> targetClass) {
        List<T> targetList = new ArrayList<>();

        for (S source : sourceList) {
            try {
                T target = copyProperties(source, targetClass);
                targetList.add(target);
            } catch (Exception e) {
                // 处理异常
                e.printStackTrace();
            }
        }

        return targetList;
    }

    /**
     * 对象是否为空
     *
     * @param obj
     * @return
     */
    public static boolean isBlank(Object obj) {

        if (null == obj) {
            return true;
        }

        if (obj instanceof String) {        // 检查是否为字符串
            return ((String) obj).isEmpty();
        } else if (obj instanceof Collection) {        // 检查是否为集合
            Collection<?> collection = (Collection<?>) obj;
            return collection.isEmpty();
        } else if (obj.getClass().isArray()) {         //是否为集合
            int length = java.lang.reflect.Array.getLength(obj);
            return length == 0;
        } else if (obj instanceof JSONArray) {
            JSONArray temp = (JSONArray) obj;
            return temp.length() == 0;
        }

        return false;
    }

    public static boolean isBlank(Object... obj) {

        boolean flag = true;
        for (Object o : obj) {
            flag = flag && isBlank(o);
        }

        return false;
    }

    /**
     * 对象不为空
     *
     * @param obj
     * @return
     */
    public static boolean isNotBlank(Object obj) {
        return !isBlank(obj);
    }

    public static boolean isNotBlank(Object... obj) {
        boolean flag = true;
        for (Object o : obj) {
            flag = flag && isNotBlank(obj);
        }
        return flag;
    }
}
